import { Component, PropsWithChildren } from 'react'
import './app.less'

// 轻量级 TextEncoder polyfill 实现
(function () {
  if (typeof TextEncoder === 'undefined') {
    console.log('Applying lightweight TextEncoder polyfill');
    
    // 简单的 TextEncoder 实现
    global.TextEncoder = class {
      encode(string: string): Uint8Array {
        const utf8Bytes: number[] = [];
        for (let i = 0; i < string.length; i++) {
          let charCode = string.charCodeAt(i);
          if (charCode < 0x80) {
            // 单字节
            utf8Bytes.push(charCode);
          } else if (charCode < 0x800) {
            // 双字节
            utf8Bytes.push(0xc0 | (charCode >> 6));
            utf8Bytes.push(0x80 | (charCode & 0x3f));
          } else if (charCode < 0xd800 || charCode >= 0xe000) {
            // 三字节
            utf8Bytes.push(0xe0 | (charCode >> 12));
            utf8Bytes.push(0x80 | ((charCode >> 6) & 0x3f));
            utf8Bytes.push(0x80 | (charCode & 0x3f));
          } else {
            // 处理代理对
            i++;
            charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (string.charCodeAt(i) & 0x3ff));
            utf8Bytes.push(0xf0 | (charCode >> 18));
            utf8Bytes.push(0x80 | ((charCode >> 12) & 0x3f));
            utf8Bytes.push(0x80 | ((charCode >> 6) & 0x3f));
            utf8Bytes.push(0x80 | (charCode & 0x3f));
          }
        }
        return new Uint8Array(utf8Bytes);
      }
    } as any as { new (): TextEncoder; prototype: TextEncoder };
    
    // 简单的 TextDecoder 实现
    global.TextDecoder = class {
      decode(buffer: ArrayBuffer | Uint8Array): string {
        const bytes = new Uint8Array(buffer);
        let result = '';
        let i = 0;
        
        while (i < bytes.length) {
          const byte1 = bytes[i++];
          let codePoint: number | undefined;
          
          if ((byte1 & 0x80) === 0) {
            // 单字节
            codePoint = byte1;
          } else if ((byte1 & 0xe0) === 0xc0) {
            // 双字节
            const byte2 = bytes[i++] & 0x3f;
            codePoint = ((byte1 & 0x1f) << 6) | byte2;
          } else if ((byte1 & 0xf0) === 0xe0) {
            // 三字节
            const byte2 = bytes[i++] & 0x3f;
            const byte3 = bytes[i++] & 0x3f;
            codePoint = ((byte1 & 0x0f) << 12) | (byte2 << 6) | byte3;
          } else if ((byte1 & 0xf8) === 0xf0) {
            // 四字节
            const byte2 = bytes[i++] & 0x3f;
            const byte3 = bytes[i++] & 0x3f;
            const byte4 = bytes[i++] & 0x3f;
            codePoint = ((byte1 & 0x07) << 18) | (byte2 << 12) | (byte3 << 6) | byte4;
          }
          
          if (codePoint !== undefined) {
            if (codePoint > 0xffff) {
              codePoint -= 0x10000;
              result += String.fromCharCode(((codePoint >>> 10) & 0x3ff) | 0xd800);
              result += String.fromCharCode(0xdc00 | (codePoint & 0x3ff));
            } else {
              result += String.fromCharCode(codePoint);
            }
          }
        }
        
        return result;
      }
    } as any as { new (): TextDecoder; prototype: TextDecoder };
  } else {
    console.log('TextEncoder already exists');
  }
})();

class App extends Component<PropsWithChildren>  {

  componentDidMount () {}

  componentDidShow () {}

  componentDidHide () {}

  // this.props.children 是将要会渲染的页面
  render () {
    return this.props.children
  }
}

export default App
